package com.pay.utils;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;

import java.io.IOException;
import java.io.StringWriter;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * 工具类
 *
 * @author MENG
 * @version 2018/7/11
 * @see
 */
public class Utils
{
    /**
     * 获取服务器IP地址
     *
     * @return
     */
    public static String getServerIp()
    {
        String localIp = null;// 本地IP，如果没有配置外网IP则返回它

        String netIp = null;// 外网IP

        try
        {
            Enumeration netInterfaces = NetworkInterface.getNetworkInterfaces();

            InetAddress ip = null;

            boolean finded = false;// 是否找到外网IP

            while (netInterfaces.hasMoreElements() && !finded)
            {
                NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();

                Enumeration address = ni.getInetAddresses();

                while (address.hasMoreElements())
                {
                    ip = (InetAddress) address.nextElement();

                    if (!ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1)
                    {
                        // 外网IP
                        netIp = ip.getHostAddress();

                        finded = true;

                        break;

                    }
                    else if (ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1)
                    {
                        // 内网IP
                        localIp = ip.getHostAddress();
                    }
                }
            }

        } catch (SocketException e)
        {
            e.printStackTrace();
        }

        if (netIp != null && !"".equals(netIp))
        {
            return netIp;
        }
        else
        {
            return localIp;
        }
    }

    public static String mapToxml(Map<String, String> map) throws IOException
    {
        Document d = DocumentHelper.createDocument();

        Element root = d.addElement("xml");

        Set<String> keys = map.keySet();

        for(String key:keys)
        {
            root.addElement(key).addText(map.get(key));
        }
        StringWriter sw = new StringWriter();

        XMLWriter xw = new XMLWriter(sw);

        xw.setEscapeText(false);

        xw.write(d);

        return sw.toString();
    }

    public static Map<String,String> xmlToMap(String xml) throws IOException
    {
        try
        {
            Map<String,String> maps = new HashMap<String, String>();

            Document document = DocumentHelper.parseText(xml);

            Element root = document.getRootElement();

            List<Element> eles = root.elements();

            for(Element e : eles)
            {
                maps.put(e.getName(), e.getTextTrim());
            }

            return maps;
        }
        catch (DocumentException e)
        {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 检查Email 是否合法
     * @param email
     * @return
     */
    public static boolean checkEmail(String email)
    {
        String regex = "^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$";

        Pattern p = Pattern.compile(regex);

        Matcher m = p.matcher(email);

        return m.matches();
    }

    /**
     * 检查phone 是否合法
     * @param phone
     * @return
     */
    public static boolean checkPhone(String phone)
    {
        String regex = "^1[3|4|5|7|8][0-9]{9}$";

        Pattern p = Pattern.compile(regex);

        Matcher m = p.matcher(phone);

        return m.matches();
    }

}
